package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.entity.Dish;
import com.sky.result.Result;
import com.sky.service.admin.DishFlavorService;
import com.sky.service.admin.DishService;
import com.sky.vo.DishVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequiredArgsConstructor
@Slf4j
@RestController("adminDishController")
@RequestMapping("/admin/dish")
public class DishController {
    private final DishService dishService;
    private final DishFlavorService dishFlavorService;

    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<DishVO> findById(@PathVariable Integer id){
        DishVO dishVO = dishService.getDishVoServiceById(id);
        return Result.success(dishVO);
    }

    /**
     * 修改菜品
     * @param dishDTO
     * @return
     */
    @PutMapping
    public Result<Object> changeDish(@RequestBody DishDTO dishDTO){
        dishService.changeDish(dishDTO);
        return Result.success();
    }


    /**
     * 菜品分页查询
     * @param categoryId
     * @param name
     * @param page
     * @param pageSize
     * @param status
     * @return
     */
    @GetMapping("/page")
    public Result<Map<String, Object>> findAllPage(
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) String name,
            @RequestParam Integer page,
            @RequestParam Integer pageSize,
            @RequestParam(required = false) Integer status
    ){
        Map<String, Object> result = dishService.findAllPage(categoryId, name, page, pageSize, status);
        log.info("查询结果：{}", result);
        return Result.success(result);
    }

    /**
     * 添加菜品
     * @param dishDTO
     * @return
     */
    @PostMapping
    public Result<Object> addDish(@RequestBody DishDTO dishDTO){
        log.info("添加菜品：{}", dishDTO);
        dishService.addDish(dishDTO);
        return Result.success();
    }

    /**
     * 批量删除菜品
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result<Object> deleteDish (@RequestParam String ids){
        try {
            dishService.deleteDishByIds(ids);
        } catch (Exception e) {
            return Result.error("添加失败："+e.getMessage());
        }
        return Result.success();
    }

    /**
     * 启用禁用分类
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    public Result<Object> enableOrDisabled
    (@PathVariable Integer status,
     @RequestParam Integer id){
        Dish dishServiceById = dishService.getById(id);
        dishServiceById.setStatus(status);
        dishService.updateById(dishServiceById);
        return Result.success();
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    public Result<List<Dish>> findAllByCategoryId(@RequestParam Integer categoryId){
        List<Dish> list = dishService.findAllByCategoryId(categoryId);
            return Result.success(list);
    }

}
